#coding:utf-8
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template.context import RequestContext
from django.contrib.auth.decorators import login_required
from lovewith.views import site_info
from lovewith.account.views.modules.userapi import UserApi
from lovewith.account.views.modules.weddingapi import WeddingApi
from lovewith.website.views.modules.province import ProvinceApi
from lovewith.supplier.views.modules.supplierapi import SupplierApi


#商家资料设置


@login_required
#商家营业信息
def edit_biz_data(request):
    if not request.user.group_id == 3:
        return HttpResponseRedirect('/')

    supplier_data = SupplierApi.check_data(user_id=int(request.user.id))

    if request.method == "GET":
        service_type = SupplierApi.get_service_type(supplier_data.id)
        service_area = SupplierApi.get_service_area(supplier_data.id)
        return render_to_response('user/setting-biz.jinja', {
            'tab': 'setting',
            'user_home': supplier_data.user,
            'is_home': True,
            'supplier_data': supplier_data,
            'service_type': service_type,
            'service_area': service_area
        }, context_instance=RequestContext(request, processors=[site_info]))
    else:
        content = request.POST.get('content')
        business_tel = request.POST.get('business_tel')
        business_time = request.POST.get('business_time')
        site = request.POST.get('site')
        weibo = request.POST.get('weibo')
        weixin = request.POST.get('weixin')
        qq = request.POST.get('qq')
        email = request.POST.get('email')

        if weibo.find('http://') == -1:
            weibo = 'http://%s' % weibo

        supplier_data.content = content
        supplier_data.business_tel = business_tel
        supplier_data.business_time = business_time
        supplier_data.site = site
        supplier_data.weibo = weibo
        supplier_data.weixin = weixin
        supplier_data.qq = qq
        supplier_data.email = email

        supplier_data.save()

        return HttpResponseRedirect('/u/setting/biz/#business-done')


#商家编辑分类
@login_required
def edit_biz_cate(request):
    if not request.user.group_id == 3:
        return HttpResponseRedirect('/')

    supplier_data = SupplierApi.check_data(user_id=int(request.user.id))
    biz_cate_data = SupplierApi.get_all_cate(supplier_data.id)

    if request.method == "GET":
        return render_to_response('user/setting-cate.jinja', {
            'tab': 'setting',
            'user_home': supplier_data.user,
            'is_home': True,
            'supplier_data': supplier_data,
            'biz_cate_data': biz_cate_data
        }, context_instance=RequestContext(request, processors=[site_info]))


#用户资料设置
@login_required
def edit_user_data(request):
    user_data = UserApi.check_data(user_id=int(request.user.id))
    supplier_data = SupplierApi.check_data(user_id=int(request.user.id))
    country_data = ProvinceApi.get_all()
    #婚礼信息
    wedding_data = WeddingApi.check_data(request.user.id)
    auth_data = UserApi.get_auth(request.user.id)

    return render_to_response('user/setting-info.jinja', {
        'tab': 'setting',
        'supplier_data': supplier_data,
        'is_home': True,
        'user_home': user_data,
        'country': country_data,
        'user': request.user,
        'auth_data': auth_data,
        'wedding_data': wedding_data
    }, context_instance = RequestContext(request, processors = [site_info]))